home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / !SFcolours / c / Menus < prev    next >
Encoding:
Text File  |  2003-10-24  |  7.9 KB  |  232 lines

  1. /*
  2.  *  SFcolours - Star Fighter 3000 colours editor
  3.  *  Menu attached to colours window (all levels)
  4.  *  Copyright (C) 2001  Chris Bazley
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public Licence as published by
  8.  *  the Free Software Foundation; either version 2 of the Licence, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public Licence for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public Licence
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. /* ANSI library files */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. /* RISC OS library files */
  27. #include "kernel.h"
  28. #include "wimp.h"
  29. #include "toolbox.h"
  30. #include "event.h"
  31. #include "wimplib.h"
  32. #include "menu.h"
  33.  
  34. /* My library files */
  35. #include "err.h"
  36. #include "msgtrans.h"
  37. #include "Macros.h"
  38. #include "Pal256.h"
  39. #include "FilePerc.h"
  40.  
  41. /* Local headers */
  42. #include "EditColmap.h"
  43. #include "SFCFileInfo.h"
  44. #include "SFCSaveBox.h"
  45. #include "Utils.h"
  46. #include "DCS_dialogue.h"
  47. #include "Main.h"
  48. #include "Menus.h"
  49.  
  50. /* Menu entries */
  51. #define FILEMENU_INFO    0x01
  52. #define FILEMENU_SAVE    0x02
  53. #define FILEMENU_DISCARD 0x00
  54.  
  55. #define EDITMENU_SETCOL  0x01
  56. #define EDITMENU_SMOOTH  0x04
  57. #define EDITMENU_SELECTALL 0x05
  58. #define EDITMENU_CLEARSEL  0x03
  59.  
  60. ObjectId ColsMenu_sharedid = NULL_ObjectId, EditMenu_sharedid = NULL_ObjectId;
  61.  
  62. /* ----------------------------------------------------------------------- */
  63. /*                       Function prototypes                               */
  64.  
  65. static ToolboxEventHandler _EditMenu_showhandler, _FileMenu_selectionhandler, _EditMenu_colourselhandler, _EditMenu_selectionhandler;
  66. static void _EditMenu_fade(ViewData *view_data);
  67.  
  68. /* ----------------------------------------------------------------------- */
  69. /*                         Public functions                                */
  70.  
  71. void ColsMenu_initialise(ObjectId id)
  72. {
  73.   ColsMenu_sharedid = id;
  74. }
  75.  
  76. /* ----------------------------------------------------------------------- */
  77.  
  78. void FileMenu_initialise(ObjectId id)
  79. {
  80.   /* Install handlers */
  81.   EF(event_register_toolbox_handler(id, Menu_Selection, _FileMenu_selectionhandler, NULL));
  82. }
  83.  
  84. /* ----------------------------------------------------------------------- */
  85.  
  86. void EditMenu_initialise(ObjectId id)
  87. {
  88.   EditMenu_sharedid = id;
  89.  
  90.   /* Install handlers */
  91.   EF(event_register_toolbox_handler(id, Menu_AboutToBeShown, _EditMenu_showhandler, NULL));
  92.   EF(event_register_toolbox_handler(id, Menu_Selection, _EditMenu_selectionhandler, NULL));
  93.   EF(event_register_toolbox_handler(pal256_sharedid, Pal256_ColourSelected, _EditMenu_colourselhandler, NULL));
  94. }
  95.  
  96. /* ----------------------------------------------------------------------- */
  97. /*                         Private functions                               */
  98.  
  99. static int _FileMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  100. {
  101.   /* Info to be extracted from ancestor Colour map window: */
  102.   ViewData *view_data;
  103.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
  104.  
  105.   switch(id_block->self_component) {
  106.     case FILEMENU_INFO:
  107.       RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, fileinfo_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  108.       return 1; /* claim event */
  109.     
  110.     case FILEMENU_SAVE:
  111.       if(strchr(view_data->last_savepath, (int)'.') == NULL) {
  112.         /* Must open savebox */
  113.         RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, savebox_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  114.       }
  115.       else {
  116.         /* Save file immediately */
  117.         _kernel_oserror *err = perc_operation(FILEPERC_OP_COMP, view_data->last_savepath, FILETYPE_FEDNET, (flex_ptr)&view_data->colour_map);
  118.         if(err != NULL)
  119.           /* Saving error */
  120.           err_report(err->errnum, msgs_lookup_sub1("SaveFail", err->errmess));
  121.         else
  122.           EditColmap_newfile(view_data, view_data->last_savepath, 1);
  123.       }
  124.       return 1; /* claim event */
  125.  
  126.     case FILEMENU_DISCARD:
  127.       if(view_data->changed_since_save) {
  128.         /* Unsaved data - prompt user for action */
  129.         dcs_openparent = false;
  130.         RE(open_topleftofwin(Toolbox_ShowObject_AsMenu, dcs_sharedid, id_block->ancestor_id, id_block->self_id, id_block->self_component))
  131.       }
  132.       else {
  133.         /* Close immediately */
  134.         RE(toolbox_delete_object(0, id_block->ancestor_id))
  135.       }
  136.       return 1; /* claim event */
  137.   }
  138.   
  139.   return 0; /* event not handled */
  140. }
  141.  
  142. /* ----------------------------------------------------------------------- */
  143.  
  144. static int _EditMenu_colourselhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  145. {
  146.   ViewData *view_data;
  147.  
  148.   if(id_block->parent_id != EditMenu_sharedid)
  149.     return 0; /* none of our business */
  150.  
  151.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 1)
  152.   EditColmap_set_selcolour(view_data, ((Pal256ColourSelectedEvent *)event)->colour_number);
  153.   
  154.   return 1; /* claim event */
  155. }
  156.  
  157. /* ----------------------------------------------------------------------- */
  158.  
  159. static int _EditMenu_selectionhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  160. {
  161.   ViewData *view_data;
  162.   E_RETV(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data), 0)
  163.  
  164.   switch(id_block->self_component) {
  165.     case EDITMENU_SETCOL:{
  166.       int sel_colour = EditColmap_get_selcolour(view_data);
  167.       if(sel_colour != -1) {
  168.         if(!E(Pal256_set_colour(pal256_sharedid, (char)sel_colour)))
  169.           RE(toolbox_show_object(Toolbox_ShowObject_AsMenu, pal256_sharedid, Toolbox_ShowObject_AtPointer, NULL, id_block->self_id, id_block->self_component))
  170.       }
  171.       else
  172.         _kernel_oswrch(7); /* beep */
  173.       }return 1; /* claim event */
  174.  
  175.     case EDITMENU_SMOOTH:
  176.       EditColmap_smoothselection(view_data); /* checks internally for insufficient selection */
  177.       return 1; /* claim event */
  178.       
  179.     case EDITMENU_SELECTALL:
  180.       EditColmap_selectall(view_data);
  181.       _EditMenu_fade(view_data);
  182.       return 1; /* claim event */
  183.  
  184.     case EDITMENU_CLEARSEL:
  185.       EditColmap_clearselection(view_data);
  186.       _EditMenu_fade(view_data);
  187.       return 1; /* claim event */
  188.   }
  189.  
  190.   return 0; /* event not handled */
  191. }
  192.  
  193. /* ----------------------------------------------------------------------- */
  194.  
  195. static int _EditMenu_showhandler(int event_code, ToolboxEvent *event, IdBlock *id_block, void *handle)
  196. {
  197.   /* Is there a selection? */
  198.   ViewData *view_data;
  199.  
  200.   if(!E(toolbox_get_client_handle(0, id_block->ancestor_id, (void **)&view_data)))
  201.     _EditMenu_fade(view_data);
  202.   
  203.   return 1; /* claim event */
  204. }
  205.  
  206. /* ----------------------------------------------------------------------- */
  207.  
  208. static void _EditMenu_fade(ViewData *view_data)
  209. {
  210.   /* Determine whether none/some/all are selected */
  211.   int num_selected = 0;
  212.   int all_sel = 1;
  213.   for(int entry = 0; entry < view_data->num_cols; entry++) {
  214.     if(view_data->sel_table[entry])
  215.       num_selected++; /* increment count of selected */
  216.     else
  217.       all_sel = 0; /* none are selected */
  218.   }
  219.  
  220.   /* If no selection exists prevent setting actual colour */
  221.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SETCOL, num_selected < 1))
  222.   
  223.   /* If less than 3 logical colours selected then prevent interpolation */
  224.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SMOOTH, num_selected < 3))
  225.  
  226.   /* If full selection then prevent select all */
  227.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_SELECTALL, all_sel))
  228.  
  229.   /* If no selection then prevent clear selection */
  230.   RE(menu_set_fade(0, EditMenu_sharedid, EDITMENU_CLEARSEL, num_selected < 1))
  231. }
  232.